﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EC.Application.Interface;
using EC.Entity.Query;
using EC.Entity.Request.PaymentRecord;
using Microsoft.AspNetCore.Mvc;

namespace EC.Merchant.Controllers
{
    /// <summary>
    /// 付款控制器
    /// </summary>
    public class PaymentrecordController : BaseController
    {
        #region 注入
        /// <summary>
        /// 付款订单接口
        /// </summary>
        private readonly PaymentRecordService _paymentRecordService;


        /// <summary>
        /// 构造函数
        /// </summary>
        public PaymentrecordController(PaymentRecordService paymentRecordService,LoginService loginService) : base(loginService)
        {
            _paymentRecordService = paymentRecordService;

        }
        #endregion

        #region 下发订单
        /// <summary>
        /// 
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        private bool IsDate(string date)
        {
            try
            {
                DateTime.Parse(date);
                return true;
            }
            catch
            {
                return false;
            }

        }
        /// <summary>
        /// 下发订单
        /// </summary>
        /// <returns>视图</returns>
        public IActionResult PaymentRecord()
        {
            ViewBag.StartTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
            ViewBag.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            return View();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<?></returns>
        public IActionResult PaymentRecordQuery(PaymentRecordQuery query)
        {
            query.UserPermission = UserPermissions;
            query.MerchantIds = new List<int>() { MerchantId };
            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
            {
                if (this.IsDate(query.StartTime) && this.IsDate(query.EndTime))
                {
                    query.StartTime = Convert.ToDateTime(query.StartTime).ToString("yyyy-MM-dd HH:mm:ss");
                    query.EndTime = Convert.ToDateTime(query.EndTime).ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    query.StartTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                }
            }
            else
            {
                query.StartTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
                query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            }

            var list = _paymentRecordService.GetMerchantPager(query);

            return Json(list);
        }

        /// <summary>
        /// 收款订单导出
        /// </summary>
        /// <returns>PagedList<?></returns>
        public JsonResult PaymentRecordExport(PaymentRecordQuery query)
        {
            query.UserPermission = UserPermissions;
            query.Page = 1;
            query.Rows = 500;
            query.MerchantIds = new List<int>() { MerchantId };


            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
            {
                if (this.IsDate(query.StartTime) && this.IsDate(query.EndTime))
                {
                    query.StartTime = Convert.ToDateTime(query.StartTime).ToString("yyyy-MM-dd HH:mm:ss");
                    query.EndTime = Convert.ToDateTime(query.EndTime).ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    query.StartTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                }
            }
            else
            {
                query.StartTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
                query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            }

            var list = _paymentRecordService.GetMerchantExportPager(query);
            list.StatusCode = $"{query.StartTime}-{query.EndTime}";
            return Json(list);
        }
        #endregion

        /// <summary>
        /// 添加代付
        /// </summary>
        /// <returns></returns>
        public IActionResult Add()
        {
            return View();
        }

        /// <summary>
        /// 添加代付
        /// </summary>
        /// <returns></returns>
        public IActionResult BatchAdd()
        {
            return View();
        }

        [HttpPost]
        public JsonResult BatchAdd([FromBody]List<BatchPaymentRecord> batchPaymentRecords)
        {
            var response = _paymentRecordService.BatchAdd(batchPaymentRecords, base.UserId, base.MerchantId);
            return Json(response);
        }
    }
}
